<?php
namespace UploadFiles;
class UploadFilesModel
{
	public function UploadFile()
	{
		$status = false;

		if ( ( $_FILES['file']['type'] == 'image/png' ) || ( $_FILES['file']['type'] == 'image/gif' )
		|| ( $_FILES['file']['type'] == 'image/jpeg' ) || ( $_FILES['file']['type'] == 'image/pjpeg' )
		|| ( $_FILES['file']['type'] == 'application/pdf' ) 
		|| ( $_FILES['file']['type'] == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ) 
		|| ( $_FILES['file']['type'] == 'application/msword' ) && ( $_FILES['file']['size'] < 1000000 ) )
		{
			$filepath = 'plugin/UploadFiles/upload/';

			if ( strstr( $_FILES['file']['type'], 'image' ) )
			{
				$filepath .= 'image/';
			}
			else if ( strstr( $_FILES['file']['type'], 'pdf' ) )
			{
				$filepath .= 'pdf/';
			}
			else if ( ( $_FILES['file']['type'] == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') || strstr( $_FILES['file']['type'], 'msword' ) )
			{
				$filepath .= 'doc/';
			}

			if ( ! file_exists( $filepath . $_FILES['file']['name'] ) )
			{
				// move the file.
				move_uploaded_file( $_FILES['file']['tmp_name'], $filepath . $_FILES['file']['name'] );
				$status = true;
			}
		}

		return $status; 
	}

	public function ScanDirectory( $directoryName )
	{
		$dir = opendir( $directoryName );
		$dirInformation = '';

		while ( $content = readdir( $dir ) )
		{
			if ( preg_match( '/^\..*$/i', $content ) )
			{
				// Skip the '.' and '...'
			}
			else
			{
				$dirInformation[] = $content;
			}
		}
		closedir( $dir );


		return $dirInformation;
	}
}